package com.voicechat.firstmet.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.voicechat.firstmet.R;
import com.voicechat.firstmet.adapter.OtherShareGirlsAdapter;
import com.voicechat.firstmet.base.BaseActivity;
import com.voicechat.firstmet.bean.MySelfInfoBean;
import com.voicechat.firstmet.bean.ShareGirlsBean;
import com.voicechat.firstmet.bean.ShareGirlsDetailsBean;
import com.voicechat.firstmet.client.OssConfig;
import com.voicechat.firstmet.net.HTTPAPI;
import com.voicechat.firstmet.net.service.FaintService;
import com.voicechat.firstmet.utils.GlideLoadUtils;
import com.voicechat.firstmet.utils.ParcelableUtils;
import com.voicechat.firstmet.utils.PhoneUtils;
import com.voicechat.firstmet.utils.SPUtils;
import com.voicechat.firstmet.utils.StatusBarUtils;
import com.voicechat.firstmet.utils.Utils;
import com.voicechat.firstmet.view.CircleImageView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.activity
 * @ClassName: OtherShareGirlsActivity
 * @Description: 他的闺蜜页面
 * @Author: ljp
 * @CreateDate: 2019/1/23 13:48
 * @Version: 1.0
 */
public class OtherShareGirlsActivity extends BaseActivity {
    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.recycler_list)
    RecyclerView recyclerList;
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout smartRefresh;
    @BindView(R.id.rela_1)
    RelativeLayout rela1;
    @BindView(R.id.circle_icon_promulgator)
    CircleImageView circleIconPromulgator;
    @BindView(R.id.tv_nike_name_promulgator)
    TextView tvNikeNamePromulgator;
    @BindView(R.id.image_sex_promulgator)
    ImageView imageSexPromulgator;
    @BindView(R.id.tv_age_promulgator)
    TextView tvAgePromulgator;
    @BindView(R.id.linear_back_age)
    LinearLayout linearBackAge;
    @BindView(R.id.image_vip)
    ImageView imageVip;
    @BindView(R.id.image_is_real)
    ImageView imageIsReal;
    @BindView(R.id.tv_autograph)
    TextView tvAutograph;
    private int page = 1;
    OtherShareGirlsAdapter shareGirlsAdapter;
    private List<ShareGirlsBean.PageBean.ListBean> tlist;
    private MySelfInfoBean.ObjectBean mySeleInfo;
    private String token;
    private String deviceInfo;
    private String TAG = "OtherShareGirlsActivity";
    private ShareGirlsDetailsBean.MapBean.MdUserBean mdUser;
    @Override
    protected int setStatusBarColor() {
        return 0;
    }
    @Override
    protected boolean isSettingStatusBarText() {
        return false;
    }

    @Override
    protected int loadLayout() {
        return R.layout.activity_other_share_girls;
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        ButterKnife.bind(this, this);
        //透明状态栏
        StatusBarUtils.Full(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        mySeleInfo = MySelfInfoBean.ObjectBean.CREATOR.createFromParcel(ParcelableUtils.unmarshall(
                SPUtils.getEditoString(SPUtils.SP_MYSELFINFO)));
        token = SPUtils.getEditoString(SPUtils.SP_TOKEN);
        deviceInfo = PhoneUtils.getDeviceInfo(context);
        mdUser = getIntent().getParcelableExtra("mdUser");
        initData();
        initView();
    }
    private void initData() {
        //发布者信息
        GlideLoadUtils.getInstance().glideLoad(context, OssConfig.OSS_HTTP + mdUser.getAvatarUrl(),
                circleIconPromulgator);
        tvNikeNamePromulgator.setText(mdUser.getNickName());
        if (mdUser.getVType() == 1) {//判断是vip等级 0非会员 1月会员 2年会员
            imageVip.setVisibility(View.VISIBLE);
            imageVip.setImageResource(R.mipmap.vip_month);
        } else if (mdUser.getVType() == 2) {
            imageVip.setVisibility(View.VISIBLE);
            imageVip.setImageResource(R.mipmap.vip_year);
        } else {
            imageVip.setVisibility(View.GONE);
        }
        if (mdUser.getGender() == 1) {//判断性别  1男 2女
            imageSexPromulgator.setImageResource(R.mipmap.boy);
            linearBackAge.setBackgroundResource(R.mipmap.sex_blue);
        } else if (mdUser.getGender() == 2) {
            imageSexPromulgator.setImageResource(R.mipmap.girl);
            linearBackAge.setBackgroundResource(R.mipmap.sex_red);
        }
        if (!TextUtils.isEmpty(mdUser.getBirthday())) {
            String substring = mdUser.getBirthday().substring(0, 4);
            String time = Utils.getStringTime(new Date());
            int age = (Integer.parseInt(time)) - (Integer.parseInt(substring));
            tvAgePromulgator.setText(age + "");
        }
        //实名认证任务 0未认证 1 已审核 2 审核中 3驳回
        if (mdUser.getIsAttestation() == 1) {
            imageIsReal.setVisibility(View.VISIBLE);
        } else {
            imageIsReal.setVisibility(View.GONE);
        }
        tvAutograph.setText(mdUser.getAutograph());
    }

    /**
     * @return
     * @method
     * @description 数据控件实例化
     * @date: 2019/1/4 20:00
     * @author: ljp
     */
    private void initView() {
        tlist = new ArrayList<>();
        smartRefresh.autoRefresh();
        //下拉刷新
        smartRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                initHttp();
                smartRefresh.finishRefresh(10000);
            }
        });
        //上拉加载更多
        smartRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                initHttp();
                smartRefresh.finishLoadMore(10000);
            }
        });
        //设置 Header 为 贝塞尔雷达 样式
        smartRefresh.setRefreshHeader(new ClassicsHeader(context));
        smartRefresh.setRefreshFooter(new ClassicsFooter(context));
        shareGirlsAdapter = new OtherShareGirlsAdapter(context, tlist, R.layout.item_share_girls, "");
        recyclerList.setLayoutManager(new GridLayoutManager(activity, 2));
        recyclerList.setAdapter(shareGirlsAdapter);
        shareGirlsAdapter.setItemClick(new OtherShareGirlsAdapter.ItemClick() {
            @Override
            public void setItemClick(int position, ShareGirlsBean.PageBean.ListBean listBean) {
                Intent intent = new Intent(activity, ShareGirlsDetailsActivity.class);
                intent.putExtra("id", listBean.getId());
                intent.putExtra("Price", listBean.getPrice());
                context.startActivity(intent);
            }
        });
        initHttp();
    }

    /**
     * @return
     * @method
     * @description 请求网络信息
     * @date: 2019/1/17 19:08
     * @author: ljp
     */
    private void initHttp() {
        FaintService.shareMyGirlsSelect(token, deviceInfo,page, mdUser.getId(),"1").subscribe(shareGirlsBean -> {
            Log.i(TAG, "initHttp: ----------" + shareGirlsBean.toString());
            if (shareGirlsBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETRUN_SUCCESS) {
                int i = shareGirlsBean.getPage().getCount() / shareGirlsBean.getPage().getPageSize() +
                        (shareGirlsBean.getPage().getCount() % shareGirlsBean.getPage().getPageSize() == 0 ? 0 : 1);
                if (i != 0 && page > i) {//判断是否是最后一页
                    smartRefresh.finishLoadMoreWithNoMoreData();//完成加载并标记没有更多数据 1.0.4
                } else {
                    if (page == 1) {
                        tlist.clear();
                        smartRefresh.finishRefresh();
                    }
                    if (shareGirlsBean.getPage().getList() != null) {
                        tlist.addAll(shareGirlsBean.getPage().getList());
                        shareGirlsAdapter.notifyDataSetChanged();
                    } else {
                    }
                    smartRefresh.finishLoadMore();
                }
            } else if (shareGirlsBean.getCode() == HTTPAPI.RETURN_CODE_TYPE.RETURN_NO_TOKEN) {
                Utils.setToken(context);
            } else {
                toast_MakeText(shareGirlsBean.getMsg());
            }
        }, throwable -> {
            Log.i(TAG, "initHttp: ----------" + throwable.toString());
            toast_MakeText(getResources().getString(R.string.net_error));
        });


    }

    @OnClick(R.id.linear_back)
    public void onClick() {
        finish();
    }
}
